<?php

class HelpController extends CustomControllerAction {
    public function init()
    {
          parent::init();
            // $cache определен в предыдущем примере
        // проверка, есть ли уже запись в кэше:
        if(!$cats = $this->cache->load('leftcat')) {
            // промах кэша
            $cats = DatabaseObject_Categories::GetAllParent($this->db);
            $this->cache->save($cats, 'leftcat');
        } else {
            // попадание в кэш
        }
          $this->view->categories = $cats;
    }

        public function indexAction() {
        $cats = DatabaseObject_HelpCategories::GetCategories($this->db);
        
        $this->view->cats = $cats;
      
        $last = DatabaseObject_HelpPages::GetLast($this->db);
        $popular = DatabaseObject_HelpPages::GetPopular($this->db);
        
        $this->view->last = $last;
        $this->view->popular = $popular;
    }
    
    public function pageAction() {
        $page = new DatabaseObject_HelpPages($this->db);
        $page->load($this->getRequest()->getParam('alias'), 'alias');
        
        $cat = new DatabaseObject_HelpCategories($this->db);
        $cat->load($page->cat_id);
        
        $this->view->cat = $cat;
        $this->view->page = $page;
      
    }
    
    public function viewcatAction() {
        $cat = new DatabaseObject_HelpCategories($this->db);
        $cat->load($this->getRequest()->getParam('name'), 'alias');
        
        $cats = DatabaseObject_HelpCategories::GetCategories($this->db, $cat->getId());
        
        $recommend = DatabaseObject_HelpPages::GetRecommend($this->db, $cat->getId());
        
        $this->view->cats = $cats;
        $this->view->category = $cat;
        $this->view->recommend = $recommend;
       
    }
    
    
    public function searchAction() {
        $query = $this->getRequest()->getQuery('q');
        
        $page = max(1, $this->getRequest()->getQuery('page'));
        
        $per_page = 10;
        $offset = ($page - 1) * $per_page;
        
        $total_found = DatabaseObject_HelpPages::GetSearchCount($this->db, $query);
        $p_count = ceil((float) $total_found / (float) $per_page);
        
        $results = DatabaseObject_HelpPages::Search($this->db, $query, $per_page, $offset);
        $pag = CustomControllerAction::Pag($p_count,$page);
        $this->view->pages = $pag;
        $this->view->p_count = $p_count;
        $this->view->cur_page = $page;
        $this->view->results = $results;
        //$this->pre($results);
        
        $this->view->query = $query;
      
    }
}